package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.HomeFragment1.RecommendBean;
import com.wnhz.work.jy.fragment.selectgoodpa.Fragment1;
import com.wnhz.work.jy.fragment.selectgoodpa.Fragment2;
import com.wnhz.work.jy.fragment.selectgoodpa.Fragment3;
import com.wnhz.work.jy.fragment.selectgoodpa.Fragment4;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择商品
 */
public class SelectGoodActivity extends BaseActivity implements View.OnClickListener {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> list_fragment = new ArrayList<>(); //定义要装fragment的列表
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    private Fragment3 fragment3;
    private Fragment4 fragment4;
    private List<String> list_title = new ArrayList<>(); //tab名称列表

    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_good);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.vPager);

        list_title.add("熊猫币");
        list_title.add("纪念币");
        list_title.add("衍生品");
        list_title.add("外国币");

        fragment1 = new Fragment1();
        fragment2 = new Fragment2();
        fragment3 = new Fragment3();
        fragment4 = new Fragment4();
        list_fragment.add(fragment1);
        list_fragment.add(fragment2);
        list_fragment.add(fragment3);
        list_fragment.add(fragment4);

        //设置TabLayout的模式
        tabLayout.setTabMode(TabLayout.MODE_FIXED);

        //为TabLayout添加tab名称
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(1)));
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(2)));
        tabLayout.addTab(tabLayout.newTab().setText(list_title.get(3)));

        MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(myViewPagerAdapter);
        viewPager.setOffscreenPageLimit(list_title.size());
        tabLayout.setupWithViewPager(viewPager);
        findViewById(R.id.btn).setOnClickListener(this);
        initTirle();
    }

    private void initTirle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("商品选择");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.btn:
                ArrayList<RecommendBean> arrayList = new ArrayList<>();
                arrayList.addAll(fragment1.getGoods());
                arrayList.addAll(fragment2.getGoods());
                arrayList.addAll(fragment3.getGoods());
                arrayList.addAll(fragment4.getGoods());
                Intent intent = new Intent();
                intent.putExtra("arrayList",arrayList);
                setResult(RESULT_OK,intent);
                finish();
                break;

        }
    }


    private class MyViewPagerAdapter extends FragmentPagerAdapter{

        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list_fragment.get(position);
        }

        @Override
        public int getCount() {
            return list_title.size();
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {
            return list_title.get(position % list_title.size());
        }
    }
}
